<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="gly"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head>
<meta charset="GB18030">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/css/font-awesome.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/static/css/main.css">

</head>
<body style="margin-top: -50px">
	<div class="panel panel-default" style="margin-top: 0px">
		<div class="panel-heading">
			<h3 class="panel-title">
				<i class="glyphicon glyphicon-th"></i> 保险记录
			</h3>
		</div>
		<div class="panel-body">
			<form class="form-inline" role="form" style="float: left;">

				<div class="form-group has-feedback">
					<div class="input-group">
						<div class="input-group-addon">车牌号码</div>
						<input class="form-control has-success" type="text"
							placeholder="支持模糊查询" id="searchCondition" /> <input
							id="condition" type="text" hidden value="" /> <input id="pn"
							type="text" hidden value="1" />
					</div>
				</div>
				<button type="button" class="btn btn-warning" id="search_btn">
					<i class="glyphicon glyphicon-search"></i> 查询
				</button>
			</form>

			<button class="btn btn-danger"
				style="float: right; margin-left: 10px;" id="delCars_btn">
				<i class=" glyphicon glyphicon-remove"></i> 批量删除
			</button>
			<button class="btn btn-info" style="float: right; margin-left: 10px;"
				id="add">
				<i class=" glyphicon glyphicon-plus"></i> 新增
			</button>
			<br>
			<hr style="clear: both;">
			<div class="table-responsive">
				<table class="table  table-bordered" id="insuranceTable">
					<thead>
						<tr>

							<th width="30"><input type="checkbox" id="selectAll" ></th>
							<th>车牌号码</th>
							<th>保险开始时间</th>
							<th>保险种类</th>
							<th>保险金额</th>
							<th>保险公司</th>
							<th>保险到期时间</th>
							<th>操作人员</th>
							<th>备注</th>
							<th width="125">操作</th>
						</tr>
					</thead>
					<!-- 信息展示 -->
					<tbody id="tb1">
					</tbody>
				</table>
			</div>
			<!-- 分页信息 -->
			<div class="row" id="page_div"></div>
		</div>
		<!-- 新增/修改模态框 -->
		<div class="modal fade" id="InsuranceModel" tabindex="-1" role="dialog"
			aria-labelledby="myModalLabel">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title" id="myModalLabel">新增保险信息</h4>
					</div>
					<div class="modal-body">
						<form class="form-horizontal" id="add_insurance_from">
							<input type="text" id="inputinId" name="inId" hidden>
							<div class="form-group">
								<label for="inputcarLicense" class="col-sm-2 control-label">车牌号码</label>							
								<div class="col-sm-10">
									<select class="form-control" id="inputinCarlicense" name="inCarlicense">

									</select>
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarOilwear" class="col-sm-2 control-label">保险开始时间</label>
								<div class="col-sm-10">
									<input type="date" class="form-control" id="inputinStarttime"
										name="inStarttime" placeholder="请输入保险到期时间）" required="required">
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarColor" class="col-sm-2 control-label">保险种类</label>
								<div class="col-sm-10">
									<select class="form-control" id="inputinSpecies" name="inSpecies">
									</select>
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarLoad" class="col-sm-2 control-label">保险金额</label>
								<div class="col-sm-10">
									<input type="text" class="form-control" id="inputinAmount"
										name="inAmount" placeholder="请输入保险金额" required="required">
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarColor" class="col-sm-2 control-label">保险公司</label>
								<div class="col-sm-10">
									<select class="form-control" id="inputinUnit" name="inUnit">
									</select>
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarOilwear" class="col-sm-2 control-label">保险到期时间</label>
								<div class="col-sm-10">
									<input type="date" class="form-control" id="inputinEndtime"
										name="inEndtime" placeholder="请输入保险到期时间）" required="required">
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarColor" class="col-sm-2 control-label">操作人员</label>
								<div class="col-sm-10">
									<select class="form-control" id="inputinDriver" name="inDriver">
									</select>
								</div>
							</div>
							<div class="form-group">
								<label for="inputcarUpkeepmileage"
									class="col-sm-2 control-label">备注</label>
								<div class="col-sm-10">
									<input type="text" class="form-control"
										id="inputinRemark" name="inRemark"
										placeholder="请输入备注信息" required="required">
								</div>
							</div>
						
						</form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" class="btn btn-primary" id="add_Insurance">保存</button>
					</div>

				</div>
			</div>
		</div>
		<!-- 新增修改模态框结束 -->
		<!-- 详细信息模态框 -->
		<div class="modal fade" id="InsuranceInfoModel" tabindex="-1" role="dialog"
			aria-labelledby="myModalLabel">
			<div class="modal-dialog" role="document">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"
							aria-label="Close">
							<span aria-hidden="true">&times;</span>
						</button>
						<h4 class="modal-title" id="myModalLabel">保险详细信息</h4>
					</div>
					<div class="modal-body">
						<table class="table  table-bordered">
							<thead>
								<tr>
									<th width="200px;">名称</th>
									<th>内容</th>

								</tr>
							</thead>
							<tbody id="info_tb">

							</tbody>
						</table>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>

				</div>
			</div>
		</div>
		<!-- 详细信息模态框结束 -->
		<script
			src="${pageContext.request.contextPath}/static/jquery/jquery-2.1.1.min.js"></script>
		<script
			src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.min.js"></script>
		<script
			src="${pageContext.request.contextPath}/static/script/docs.min.js"></script>
		
		<script type="text/javascript">
			$(function(){

				loadInsurance();
				
				addInsurance();
				
				
				
				//查询数据
				$("#search_btn").click(function(){
					var searchcondition = $("#searchCondition").val();
					$("#condition").val(searchcondition);
					$("#pn").val(1);
					loadInsurance();
				})
				
				//复选框的选中与取消
				$("#selectAll").change(function() {
					if ($(this).prop('checked') == true) {
						$('input[name="inid"]').each(function() {
							$(this).prop('checked', true);
						})
					} else {
						$('input[name="inid"]').each(function() {
							$(this).prop('checked', false);
						})
					}

				});
				
			});
			
			
		
		
		
		
		//数据展示
		function loadInsurance(){
//			alert("加载数据");
			var pn = $("#pn").val();
			var condition = $("#condition").val();
			
			$.ajax({
				url:"${pageContext.request.contextPath}/insurance/getAll",
				type:"post",
				dataType:"json",
				data : {
					"pn" : pn,
					"condition" : condition
				},
				error:function(){
					alert(789);
				},
				success:function(data){
//					alert(data);
					var list = data.list;
					var trStr="" ;
					for(var i=0;i<list.length;i++){
						
						trStr=trStr+"<tr>";
						trStr=trStr+"<td><input type='checkbox' name='inid' value='"+list[i].inId+"'/></td>";
						
						trStr=trStr+"<td>"+list[i].inCarlicense+"</td>";
						trStr=trStr+"<td>"+list[i].inStarttime+"</td>";
						trStr=trStr+"<td>"+list[i].inSpecies+"</td>";
						trStr=trStr+"<td>"+list[i].inAmount+"</td>";
						trStr=trStr+"<td>"+list[i].inUnit+"</td>";
						trStr=trStr+"<td>"+list[i].inEndtime+"</td>";
						trStr=trStr+"<td>"+list[i].inDriver+"</td>";
						trStr=trStr+"<td>"+list[i].inRemark+"</td>";
						trStr=trStr+"<td>";

						trStr = trStr
						+ "<button type='button' class='btn btn-primary  btn-xs' name='updateInsurance' insuranceid='"+list[i].inId+"'><i class=' glyphicon glyphicon-pencil'></i></button>";
						trStr = trStr + "&nbsp";
						trStr = trStr
						+ "<button type='button' class='btn btn-danger   btn-xs' name='delInsurance' insuranceid='"+list[i].inId+"'><i class=' glyphicon glyphicon-remove'></i></button>";
						trStr = trStr + "&nbsp";
						
						trStr=trStr+"</td>";
						trStr=trStr+"</tr>";
					}
					$("#tb1").html(trStr);
					
					var divStr = "";
					divStr = divStr + "<div align='center'>";
					divStr = divStr + "<ul class='pagination'>";
					divStr = divStr
							+ "<li><a href='javascript:changePage(1)'>首页</a></li>";
					divStr = divStr + "<li>";
					if (data.hasPreviousPage) {
						divStr = divStr
								+ "<a href='javascript:changePage(-1)' aria-label='Previous'> <span aria-hidden='true'>«</span></a>";
					}
					divStr = divStr + "</li>";
					var navigatepageNums = data.navigatepageNums;
					for (var i = 0; i < navigatepageNums.length; i++) {
						if (navigatepageNums[i] == data.pageNum) {
							divStr = divStr
									+ "<li class='active'><a href='#'>"
									+ navigatepageNums[i]
									+ "</a></li>";
						}
						if (navigatepageNums[i] != data.pageNum) {
							divStr = divStr
									+ "<li><a href='javascript:changePage("
									+ navigatepageNums[i] + ")'>"
									+ navigatepageNums[i]
									+ "</a></li>";
						}
					}
					divStr = divStr + "<li>";
					if (data.hasNextPage) {
						divStr = divStr
								+ "<a href='javascript:changePage(-2)' aria-label='Next'> <span aria-hidden='true'>»</span> </a>";
					}
					divStr = divStr + "</li>";

					divStr = divStr
							+ "<li><a href='javascript:changePage("
							+ data.pages + ")'>尾页</a></li>";
					divStr = divStr + "</ul>";
					divStr = divStr + "</div>";
					divStr = divStr + "<div align='center'>当前第 "
							+ data.pageNum + " 页.总共 " + data.pages
							+ " 页.一共 " + data.total + " 条记录</div>";
					$("#page_div").html(divStr);
					
					updateIn();
					
					deletein();
					
					deleteIns();
				}
			});
		}
		
		
		//翻页
		function changePage(page) {
			var pn = $("#pn").val();
			if (page == -1) {
				$("#pn").val(pn - 1);
			} else if (page == -2) {
				var pn1 = parseInt(pn);
				$("#pn").val(pn1 + 1);
			} else {
				$("#pn").val(page);
			}

			loadInsurance();
		}
		
		
		function addInsurance(){
			//新增模块
			$("#add").click(function(){

				$("#myModalLabel").html("新增保险信息单");
				$.ajax({
					url:"${pageContext.request.contextPath}/insurance/inadd",
					type:"post",
					async:true,
					dataType:"json",
					error:function(){
						alert("找不到数据");
					},
					success:function(data){
	
						var inCarlicense=data.inCarlicense;
						var inUnit=data.inUnit;
						var inDriver=data.inDriver;
						var inSpecies=data.inSpecies;
						$("#inputinCarlicense").prop(
								"readonly", false);
						var inCarlicenseStr="";
						for (var i = 0; i < inCarlicense.length; i++) {
							inCarlicenseStr = inCarlicenseStr
									+ "<option>"
									+ inCarlicense[i]
									+ "</option>";
						}
						$("#inputinCarlicense").html(
								inCarlicenseStr);
						
						var inUnitStr="";
						for (var i = 0; i < inUnit.length; i++) {
							inUnitStr=inUnitStr
							+ "<option>"
							+ inUnit[i]
							+ "</option>";
						}
						$("#inputinUnit").html(
								inUnitStr);
						
						var inDriverStr="";
						for (var i = 0; i < inUnit.length; i++) {
							inDriverStr=inDriverStr
							+ "<option>"
							+ inDriver[i]
							+ "</option>";
						}
						$("#inputinDriver").html(
								inDriverStr);
						
						var inSpeciesStr="";
						for (var i = 0; i < inUnit.length; i++) {
							inSpeciesStr=inSpeciesStr
							+ "<option>"
							+ inSpecies[i]
							+ "</option>";
						}
						$("#inputinSpecies").html(
								inSpeciesStr);
						
						var da=new Date().format('yyyy-MM-dd');
						$("#inputinStarttime").val(da);
						var da1=new Date();
						var year=da1.getFullYear();
						da1.setFullYear(year+1);
						var da3=da1.format('yyyy-MM-dd');
						$("#inputinEndtime").val(da3);
						
					}
				});
				$('#InsuranceModel').modal('show');
			})
			
			
			//保存新增或者修改
			$("#add_Insurance").click(function(){
				if ($("#inputinCarlicense").prop("readonly") == true) {

					$.ajax({
						url : "${pageContext.request.contextPath}/insurance/doupdate",
						type : "post",
						data : $("#add_insurance_from")
								.serialize(),
						error : function() {
							alert("修改出错");
						},
						success : function(data) {
							$('#InsuranceModel').modal(
							'hide');
							loadInsurance();

						}
					});
				}else {
					$.ajax({
						url : "${pageContext.request.contextPath}/insurance/doadd",
						type : "post",
						data : $("#add_insurance_from")
								.serialize(),
						error : function() {
							alert("新增出错");
						},
						success : function(data) {
							$('#InsuranceModel').modal(
							'hide');
							loadInsurance();

						}
					});
				}
				
					
				
			});
			$('#InsuranceModel').on('hidden.bs.modal', function() {
				$("#add_insurance_from")[0].reset();
			})
			
 		}
		
		//单条删除
		function deletein(){

			$("[name=delInsurance]").click(function(){
		
				var inId = $(this).attr("insuranceid");
				if (confirm("是否删除")) {
					$.ajax({
						url : "${pageContext.request.contextPath}/insurance/delete",
						type : "post",
						data : {
							"inId" : inId
						},
						success : function(data) {
							loadInsurance();
						}
					})
				}
					
				
				
			});
		}
		
		//批量删除
		function deleteIns(){
			
			$("#delCars_btn").click(function(){
				var arr =[];
				$(":checkbox[name='inid']:checked").each(function(){
					arr.push($(this).val());	
					alert(arr);
				})
				if(arr.length==0){
					alert("请选择删除记录");
					return;
				}
				if(confirm("是否删除")){
					$.ajax({
						url : "${pageContext.request.contextPath}/insurance/deleteIns",
						type : "post",
						data : {
							"inIds":arr.join(",")
						},
						success : function(data) {
							loadInsurance();
						},
					})
				}

				
			});
			
		}
		
		
		//修改信息单
		function updateIn(){

			$("[name=updateInsurance]").click(function(){
				$("#myModalLabel").html("修改保险信息单");
				var inId = $(this).attr("insuranceid");
				$.ajax({
					url : "${pageContext.request.contextPath}/insurance/update?inIds="
						+ inId,
				type : "get",
				dataType : "json",
				success : function(data) {
					$("#inputinCarlicense").prop(
							"readonly",true);

					var inCarlicense=data.inCarlicense;
					var inUnit=data.inUnit;
					var inDriver=data.inDriver;
					var inSpecies=data.inSpecies;
					var ins=data.ins;

					
					var inCarlicenseStr="";
					for (var i = 0; i < inCarlicense.length; i++) {
						if (inCarlicense[i]==ins.inCarlicense) {
							inCarlicenseStr = inCarlicenseStr
							+ "<option selected>"
							+ inCarlicense[i]
							+ "</option>";
						}else {
							inCarlicenseStr = inCarlicenseStr
							+ "<option>"
							+ inCarlicense[i]
							+ "</option>";
						}
						
					}
					$("#inputinCarlicense").html(
							inCarlicenseStr);
					
					
					var inUnitStr="";
					for (var i = 0; i < inUnit.length; i++) {
						if(inUnit[i]==ins.inUnit){
							inUnitStr=inUnitStr
							+ "<option selected>"
							+ inUnit[i]
							+ "</option>";
						}else{
							inUnitStr=inUnitStr
							+ "<option>"
							+ inUnit[i]
							+ "</option>";
						}						
					}
					$("#inputinUnit").html(
							inUnitStr);
					
					var inDriverStr="";
					for (var i = 0; i < inUnit.length; i++) {
						if (inDriver[i]==ins.inDriver) {
							inDriverStr=inDriverStr
							+ "<option selected>"
							+ inDriver[i]
							+ "</option>";
						}else {
							inDriverStr=inDriverStr
							+ "<option>"
							+ inDriver[i]
							+ "</option>";
						}						
					}
					$("#inputinDriver").html(
							inDriverStr);
					
					var inSpeciesStr="";
					for (var i = 0; i < inUnit.length; i++) {
						if (inSpecies[i]==ins.inSpecies) {
							inSpeciesStr=inSpeciesStr
							+ "<option selected>"
							+ inSpecies[i]
							+ "</option>";
						}else {
							inSpeciesStr=inSpeciesStr
							+ "<option>"
							+ inSpecies[i]
							+ "</option>";
						}						
					}
					$("#inputinSpecies").html(
							inSpeciesStr);
					
					$("#inputinId").val(ins.inId);
					$("#inputinStarttime").val(ins.inStarttime);
					$("#inputinAmount").val(ins.inAmount);
					$("#inputinEndtime").val(ins.inEndtime);
					$("#inputinRemark").val(ins.inRemark);
					
					var year=ins.inStarttime.getFullYear();

					var month=ins.inStarttime.getMonth();
					var day=ins.inStarttime.getDate();
					var da1=new Date();
					var year=da1.getFullYear();
					da1.setFullYear(year+1);
					da1.setMonth(month);
					da1.setDate(day);
					var da3=da1.format('yyyy-MM-dd');
					$("#inputinEndtime").val(da3);
					

					

				}
				});
				$('#InsuranceModel').modal('show');
			})
		}
		
		Date.prototype.format = function(format){
		    var o = {
		        "M+" : this.getMonth()+1, //month
		        "d+" : this.getDate(), //day
		        "H+" : this.getHours(), //hour
		        "m+" : this.getMinutes(), //minute
		        "s+" : this.getSeconds(), //second
		        "q+" : Math.floor((this.getMonth()+3)/3), //quarter
		        "S" : this.getMilliseconds() //millisecond
		    }
		 
		    if(/(y+)/.test(format)) {
		        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
		    }
		 
		    for(var k in o) {
		        if(new RegExp("("+ k +")").test(format)) {
		            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
		        }
		    }
		    return format;
		}
		</script>
</body>
</html>